// RMSAddCameraDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Resource.h"

#ifndef VC10
	#include "../RMSClient/LivePage.h"
	#include "../RMSClient/StoredPage.h"
	#include "../RMSLib/info.h"
#endif

#include "RMSEditCameraDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRMSEditCameraDlg dialog


CRMSEditCameraDlg::CRMSEditCameraDlg(ClientInterface* pClient, CWnd* pParent /*=NULL*/)
	: CDialog(CRMSEditCameraDlg::IDD, pParent)
{
	setClient(pClient);

	stSite="";stBuilding="";stRoom="";stCamera="";
	//{{AFX_DATA_INIT(CRMSEditCameraDlg)
	m_nBitrate = 500;
	m_strBuilding = _T("");
	m_strCamera = _T("");
	m_nCameraID = 0;
	m_nPort = 777;
	m_nFrameRate = 15;
	m_nHeight = 240;
	m_nQuality = 0;
	m_strRoom = _T("");
	m_strSite = _T("");
	m_nWidth = 320;
	m_strPassword = _T("");
	//}}AFX_DATA_INIT

	m_liveCamName = _T("live_cameras.txt");
	m_storedCamName = _T("stored_cameras.txt");
}

void CRMSEditCameraDlg::SetLiveCamName(CString name)
{
	m_liveCamName = name;
}

FILE* CRMSEditCameraDlg::getLiveCamFile(LPCTSTR access)
{
	FILE* f = NULL;

	CString hp = getHomePath();
	if(hp.IsEmpty())
	{
		f = fopen(m_liveCamName, access);
		if(!f)
		{
			hp = "c:"; // default
			saveHomePath(hp);
		}
	}

	if(!f)
	{
		hp += "\\";
		hp += m_liveCamName;
		f = fopen(hp, access);
	}

	return f;
}

void CRMSEditCameraDlg::SetStoredCamName(CString name)
{
	m_storedCamName = name;
}

FILE* CRMSEditCameraDlg::getStoredCamFile(LPCTSTR access) 
{
	FILE* f = NULL;

	CString hp = getHomePath();
	if(hp.IsEmpty())
	{
		f = fopen(m_storedCamName, access);
		if(!f)
		{
			hp = "c:"; // default
			saveHomePath(hp);
		}
	}

	if(!f)
	{
		hp += "\\";
		hp += m_storedCamName;
		f = fopen(hp, access);
	}

	return f;
}


void CRMSEditCameraDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRMSEditCameraDlg)
	DDX_Control(pDX, IDC_PASSWORD, m_editPassword);
	DDX_Control(pDX, IDSUBMIT, m_btnSubmit);
	DDX_Control(pDX, IDDELETE, m_btnDelete);
	DDX_Control(pDX, IDCLEAR, m_btnClear);
	DDX_Control(pDX, IDADDCAMERA, m_btnAddCamera);
	DDX_Control(pDX, IDC_WIDTH, m_editWidth);
	DDX_Control(pDX, IDC_QUALITY, m_editQuality);
	DDX_Control(pDX, IDC_HEIGHT, m_editHeight);
	DDX_Control(pDX, IDC_FRAMERATE, m_editFrameRate);
	DDX_Control(pDX, IDC_CAMERAPORT, m_editPort);
	DDX_Control(pDX, IDC_CAMERAID, m_editCameraID);
	DDX_Control(pDX, IDC_BITRATE, m_editBitrate);
	DDX_Control(pDX, IDC_TYPE, m_cmbxType);
	DDX_Control(pDX, IDC_SITE, m_cmbxSite);
	DDX_Control(pDX, IDC_ROOM, m_cmbxRoom);
	DDX_Control(pDX, IDC_CAMERA, m_cmbxCamera);
	DDX_Control(pDX, IDC_BUILDING, m_cmbxBuilding);
	DDX_Control(pDX, IDC_CAMERAIP, m_ipCamera);
	DDX_Text(pDX, IDC_BITRATE, m_nBitrate);
#ifdef _J707_ // merge from VC-XR2:#103R210009:KAZUHY
	DDV_MinMaxUInt(pDX, m_nBitrate, 0, 100000);
#else
	DDV_MinMaxUInt(pDX, m_nBitrate, 0, 10000);
#endif
	DDX_CBString(pDX, IDC_BUILDING, m_strBuilding);
	DDX_CBString(pDX, IDC_CAMERA, m_strCamera);
	DDX_Text(pDX, IDC_CAMERAID, m_nCameraID);
	DDV_MinMaxUInt(pDX, m_nCameraID, 0, 10000);
	DDX_Text(pDX, IDC_CAMERAPORT, m_nPort);
	DDV_MinMaxUInt(pDX, m_nPort, 500, 65000);
	DDX_Text(pDX, IDC_FRAMERATE, m_nFrameRate);
	DDV_MinMaxUInt(pDX, m_nFrameRate, 1, 30);
	DDX_Text(pDX, IDC_HEIGHT, m_nHeight);
	DDV_MinMaxUInt(pDX, m_nHeight, 240, 480);
	DDX_Text(pDX, IDC_QUALITY, m_nQuality);
	DDV_MinMaxUInt(pDX, m_nQuality, 0, 10);
	DDX_CBString(pDX, IDC_ROOM, m_strRoom);
	DDX_CBString(pDX, IDC_SITE, m_strSite);
	DDX_Text(pDX, IDC_WIDTH, m_nWidth);
	DDV_MinMaxUInt(pDX, m_nWidth, 320, 640);
	DDX_Text(pDX, IDC_PASSWORD, m_strPassword);
	DDV_MaxChars(pDX, m_strPassword, 128);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CRMSEditCameraDlg, CDialog)
	//{{AFX_MSG_MAP(CRMSEditCameraDlg)
	ON_CBN_EDITCHANGE(IDC_SITE, OnEditChangeSite)
	ON_CBN_EDITCHANGE(IDC_BUILDING, OnEditChangeBuilding)
	ON_CBN_EDITCHANGE(IDC_ROOM, OnEditChangeRoom)
	ON_CBN_EDITCHANGE(IDC_CAMERA, OnEditChangeCamera)
	ON_CBN_SELENDOK(IDC_SITE, OnSelendokSite)
	ON_CBN_SELENDOK(IDC_BUILDING, OnSelendokBuilding)
	ON_CBN_SELENDOK(IDC_ROOM, OnSelendokRoom)
	ON_CBN_SELENDOK(IDC_CAMERA, OnSelendokCamera)
	ON_BN_CLICKED(IDADDCAMERA, OnAddcamera)
	ON_BN_CLICKED(IDCLEAR, OnClear)
	ON_EN_CHANGE(IDC_BITRATE, EnableSubmit)
	ON_CBN_SELENDOK(IDC_TYPE, OnSelendokType)
	ON_BN_CLICKED(IDDELETE, OnDelete)
	ON_BN_CLICKED(IDSUBMIT, OnSubmit)
	ON_NOTIFY(IPN_FIELDCHANGED, IDC_CAMERAIP, OnFieldchangedCameraip)
	ON_EN_KILLFOCUS(IDC_BITRATE, OnKillfocus)
	ON_EN_CHANGE(IDC_CAMERAID, EnableSubmit)
	ON_EN_CHANGE(IDC_CAMERAPORT, EnableSubmit)
	ON_EN_CHANGE(IDC_FRAMERATE, EnableSubmit)
	ON_EN_CHANGE(IDC_HEIGHT, EnableSubmit)
	ON_EN_CHANGE(IDC_QUALITY, EnableSubmit)
	ON_EN_CHANGE(IDC_WIDTH, EnableSubmit)
	ON_EN_KILLFOCUS(IDC_CAMERAID, OnKillfocus)
	ON_EN_KILLFOCUS(IDC_CAMERAPORT, OnKillfocus)
	ON_EN_KILLFOCUS(IDC_FRAMERATE, OnKillfocus)
	ON_EN_KILLFOCUS(IDC_HEIGHT, OnKillfocus)
	ON_EN_KILLFOCUS(IDC_QUALITY, OnKillfocus)
	ON_EN_KILLFOCUS(IDC_WIDTH, OnKillfocus)
	ON_EN_CHANGE(IDC_PASSWORD, EnableSubmit)
	ON_EN_KILLFOCUS(IDC_PASSWORD, OnKillfocus)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRMSEditCameraDlg message handlers

BOOL CRMSEditCameraDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
#ifdef VC10
	m_bTypeCamera = TRUE;
#endif
	m_ipCamera.SetAddress(127,0,0,1);
	UpdateData(FALSE);

	m_btnClear.ShowWindow(FALSE);

	m_cmbxType.ResetContent();
	m_cmbxSite.ResetContent();
	m_cmbxBuilding.ResetContent();
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	m_cmbxBuilding.EnableWindow(FALSE);
	m_cmbxRoom.EnableWindow(FALSE);
	m_cmbxCamera.EnableWindow(FALSE);

	m_bEnableSubmit = FALSE;

	SetCurState(FALSE);

	m_cmbxType.AddString("Live");
	m_cmbxType.AddString("Stored");

	m_cmbxType.SetCurSel(m_cmbxType.FindString(-1,"Live"));

	map<CString,CSite>::const_iterator s;

	m_cmbxSite.AddString("none");

	map<CString, CSite>* ls = getSitesLive();
	if(ls)
	{
		for(s = ls->begin(); s != ls->end(); ++s)
		{
			LPCTSTR a=(LPCTSTR)s->first;
			m_cmbxSite.AddString(a);
		}
	}

	m_cmbxSite.SetCurSel(m_cmbxSite.FindStringExact(-1, "none"));

#ifndef VC10
	for(int i = 0; i<4; i++)
	{
		CChannelInfo* ci = getInfoLive(i);
		if(ci)
		{
			ci->m_bAdd = false;
			ci->m_bChange = false;
			ci->m_bDelete = false;
		}

		CChannelInfo* si = getInfoStored(i);
		if(si)
		{
			si->m_bAdd = false;
			si->m_bChange = false;
			si->m_bDelete = false;
		}
	}
#endif
	int result;
	if (stSite!="") result=m_cmbxSite.SelectString(0,stSite);
	OnSelendokSite();
	if (stBuilding!="") result=m_cmbxBuilding.SelectString(0,stBuilding);
	OnSelendokBuilding();
	if (stRoom!="") result=m_cmbxRoom.SelectString(0,stRoom);
	OnSelendokRoom();
	if (stCamera!="") result=m_cmbxCamera.SelectString(0,stCamera);
	OnSelendokCamera();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

// return "Site Building Room Camera"
CString CRMSEditCameraDlg::GetSelectedSite()
{
	CString result;
	result.Format("%s %s %s %s",m_strSite,m_strBuilding,m_strRoom,m_strCamera);
	return result;
}
BOOL CRMSEditCameraDlg::SelectCamera(char* Site,char* Building,char* Room,char* Camera)
{
	stSite=Site;
	stBuilding=Building;
	stRoom=Room;
	stCamera=Camera;

	return TRUE;
}

void CRMSEditCameraDlg::SetCurState(BOOL bState, BOOL bEdit)
{
	m_editBitrate.EnableWindow(bState);
	m_editCameraID.EnableWindow(bState);
	m_editFrameRate.EnableWindow(bState);
	m_editHeight.EnableWindow(bState);
	m_editWidth.EnableWindow(bState);
	m_editPort.EnableWindow(bState);
	m_editQuality.EnableWindow(bState);
	m_ipCamera.EnableWindow(bState);
	m_editPassword.EnableWindow(bState);

	if(bState)
	{
		if(bEdit)
		{
			m_btnAddCamera.EnableWindow(FALSE);
			m_btnClear.EnableWindow(FALSE);
			m_btnDelete.EnableWindow(TRUE);

			m_bEdit = TRUE;
		}
		else
		{
			m_btnAddCamera.EnableWindow(TRUE);
			m_btnClear.EnableWindow(TRUE);
			m_btnDelete.EnableWindow(FALSE);

			m_bEdit = FALSE;
		}
	}
	else
	{
		m_btnAddCamera.EnableWindow(FALSE);
		m_btnClear.EnableWindow(FALSE);
		m_btnDelete.EnableWindow(FALSE);
	}

	if(m_bEnableSubmit)
	{
		m_btnSubmit.EnableWindow(TRUE);
	}
	else
	{
		m_btnSubmit.EnableWindow(FALSE);
	}
}



void CRMSEditCameraDlg::OnEditChangeSite() 
{
	UpdateData(TRUE);
	m_cmbxBuilding.ResetContent();
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	if((m_strSite != "") && (m_strSite != "none"))
	{
		m_cmbxBuilding.EnableWindow(TRUE);
		int n;
		if((n = m_cmbxSite.FindStringExact(-1,m_strSite)) != CB_ERR)
		{
			m_cmbxSite.SetCurSel(n);
			SetCurSite();
		}
		else
		{
			m_cmbxBuilding.AddString("none");
			m_cmbxBuilding.SetCurSel(m_cmbxBuilding.FindStringExact(-1,"none"));
		}
	}
	else
	{
		m_cmbxBuilding.EnableWindow(FALSE);
	}
	m_cmbxRoom.EnableWindow(FALSE);
	m_cmbxCamera.EnableWindow(FALSE);
	SetCurState(FALSE);
}

void CRMSEditCameraDlg::OnEditChangeBuilding() 
{
	UpdateData(TRUE);

	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();
	if((m_strBuilding != "") && (m_strBuilding != "none"))
	{
		m_cmbxRoom.EnableWindow(TRUE);

		int n;
		if((n = m_cmbxBuilding.FindStringExact(-1,m_strBuilding)) != CB_ERR)
		{
			m_cmbxBuilding.SetCurSel(n);
			SetCurBuilding();
		}
		else
		{
			m_cmbxRoom.AddString("none");
			m_cmbxRoom.SetCurSel(m_cmbxRoom.FindStringExact(-1,"none"));
		}
	}
	else
	{
		m_cmbxRoom.EnableWindow(FALSE);
	}
	m_cmbxCamera.EnableWindow(FALSE);
	SetCurState(FALSE);
}

void CRMSEditCameraDlg::OnEditChangeRoom() 
{
	UpdateData(TRUE);

	m_cmbxCamera.ResetContent();
	if((m_strRoom != "") && (m_strRoom != "none"))
	{
		m_cmbxCamera.EnableWindow(TRUE);

		int n;
		if((n = m_cmbxRoom.FindStringExact(-1,m_strRoom)) != CB_ERR)
		{
			m_cmbxRoom.SetCurSel(n);
			SetCurRoom();
		}
		else
		{
			m_cmbxCamera.AddString("none");
			m_cmbxCamera.SetCurSel(m_cmbxCamera.FindStringExact(-1,"none"));
		}
	}
	else
	{
		m_cmbxCamera.EnableWindow(FALSE);
	}
	SetCurState(FALSE);
}

void CRMSEditCameraDlg::OnEditChangeCamera() 
{
	UpdateData(TRUE);

	if((m_strCamera != "") && (m_strCamera != "none"))
	{
		int n;
		if((n = m_cmbxCamera.FindStringExact(-1,m_strCamera)) != CB_ERR)
		{
			m_cmbxCamera.SetCurSel(n);
			SetCurCamera();

			m_bEnableSubmit = TRUE;
			SetCurState(TRUE, TRUE);
		}
		else
		{
			SetCurState(TRUE, FALSE);
		}
	}
	else
	{
		SetCurState(FALSE);
	}
}

void CRMSEditCameraDlg::OnSelendokSite() 
{
	m_cmbxBuilding.ResetContent();
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	SetCurSite();

	m_cmbxRoom.EnableWindow(FALSE);
	m_cmbxCamera.EnableWindow(FALSE);
	SetCurState(FALSE);
}

void CRMSEditCameraDlg::OnSelendokBuilding() 
{
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	SetCurBuilding();

	m_cmbxCamera.EnableWindow(FALSE);
	SetCurState(FALSE);
}

void CRMSEditCameraDlg::OnSelendokRoom() 
{
	m_cmbxCamera.ResetContent();

	SetCurRoom();

	SetCurState(FALSE);
}

void CRMSEditCameraDlg::OnSelendokCamera() 
{
	SetCurCamera();
}

void CRMSEditCameraDlg::SetCurSite()
{
	m_cmbxBuilding.ResetContent();
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	CString site = "none";
	int nCurrentSite = m_cmbxSite.GetCurSel();
	if(nCurrentSite >= 0)
		m_cmbxSite.GetLBText(nCurrentSite, site);

	m_strSite = site;
	UpdateData(FALSE);
	
	if(site == "none")
	{
		m_cmbxBuilding.EnableWindow(FALSE);
	}
	else
	{
		map<CString,CBuilding>::const_iterator b;

		CSite* pCurrentSite;
		if(m_bTypeCamera)
		{
			pCurrentSite = getLiveSite(site);
		}
		else
		{
			pCurrentSite = getStoredSite(site);
		}

		m_cmbxBuilding.AddString("none");
		for(b = pCurrentSite->m_Buildings.begin(); b != pCurrentSite->m_Buildings.end(); ++b)
		{
			LPCTSTR a=(LPCTSTR)b->first;
			m_cmbxBuilding.AddString(a);
		}
		m_cmbxBuilding.EnableWindow(TRUE);
		m_cmbxBuilding.SetCurSel(m_cmbxBuilding.FindStringExact(-1, "none"));
	}
}

void CRMSEditCameraDlg::SetCurBuilding()
{
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	CString building = "none";
	int nCurrentBuilding = m_cmbxBuilding.GetCurSel();
	if(nCurrentBuilding >= 0)
		m_cmbxBuilding.GetLBText(nCurrentBuilding, building);

	m_strBuilding = building;
	UpdateData(FALSE);

	if(building == "none")
	{
		m_cmbxRoom.EnableWindow(FALSE);
	}
	else
	{
		map<CString,CRoom>::const_iterator r;

		CString site = "none";
		int nCurrentSite = m_cmbxSite.GetCurSel();
		if(nCurrentSite >= 0)
			m_cmbxSite.GetLBText(nCurrentSite, site);
		CSite* pCurrentSite;
		if(m_bTypeCamera)
		{
			pCurrentSite = getLiveSite(site);
		}
		else
		{
			pCurrentSite = getStoredSite(site);
		}
		
		CBuilding* pCurrentBuilding = &(pCurrentSite->m_Buildings[building]);

		m_cmbxRoom.AddString("none");
		for(r = pCurrentBuilding->m_Rooms.begin(); r != pCurrentBuilding->m_Rooms.end(); ++r)
		{
			m_cmbxRoom.AddString((LPCTSTR)r->first);
		}
		m_cmbxRoom.EnableWindow(TRUE);
		m_cmbxRoom.SetCurSel(m_cmbxRoom.FindStringExact(-1, "none"));
	}
}

void CRMSEditCameraDlg::SetCurRoom()
{
	m_cmbxCamera.ResetContent();

	CString room = "none";
	int nCurrentRoom = m_cmbxRoom.GetCurSel();
	if(nCurrentRoom >= 0)
		m_cmbxRoom.GetLBText(nCurrentRoom, room);

	m_strRoom = room;
	UpdateData(FALSE);

	if(room == "none")
	{
		m_cmbxCamera.EnableWindow(FALSE);
	}
	else
	{
		map<CString,CInfo>::const_iterator c;

		CString site = "none";
		int nCurrentSite = m_cmbxSite.GetCurSel();
		if(nCurrentSite >= 0)
			m_cmbxSite.GetLBText(nCurrentSite, site);
		CSite* pCurrentSite;
		if(m_bTypeCamera)
		{
			pCurrentSite = getLiveSite(site);
		}
		else
		{
			pCurrentSite = getStoredSite(site);
		}

		CString building = "none";
		int nCurrentBuilding = m_cmbxBuilding.GetCurSel();
		if(nCurrentBuilding >= 0)
			m_cmbxBuilding.GetLBText(nCurrentBuilding, building);

		CBuilding* pCurrentBuilding = &(pCurrentSite->m_Buildings[building]);

		CRoom* pCurrentRoom = &(pCurrentBuilding->m_Rooms[room]);

		m_cmbxCamera.AddString("none");
		for(c = pCurrentRoom->m_Cameras.begin(); c != pCurrentRoom->m_Cameras.end(); ++c)
		{
			m_cmbxCamera.AddString((LPCTSTR)c->first);
		}

		m_cmbxCamera.EnableWindow(TRUE);
		m_cmbxCamera.SetCurSel(m_cmbxCamera.FindStringExact(-1, "none"));
	}
}

void CRMSEditCameraDlg::SetCurCamera()
{
	CString camera = "none";
	int nCurrentCamera = m_cmbxCamera.GetCurSel();
	if(nCurrentCamera >= 0)
		m_cmbxCamera.GetLBText(nCurrentCamera, camera);
	
	if(camera != "none")
	{
		CString site = "none";
		int nCurrentSite = m_cmbxSite.GetCurSel();
		if(nCurrentSite >= 0)
			m_cmbxSite.GetLBText(nCurrentSite, site);

		CString building = "none";
		int nCurrentBuilding = m_cmbxBuilding.GetCurSel();
		if(nCurrentBuilding >= 0)
			m_cmbxBuilding.GetLBText(nCurrentBuilding, building);

		CString room = "none";
		int nCurrentRoom = m_cmbxRoom.GetCurSel();
		if(nCurrentRoom >= 0)
			m_cmbxRoom.GetLBText(nCurrentRoom, room);

		UpdateData(TRUE);

		m_strCamera = camera;

		if(m_bTypeCamera)
		{
			CSite* ls = getLiveSite(site);
			if(ls)
			{
				DWORD inet_address = inet_addr((LPCTSTR)ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_ip);
				BYTE* pbIP = (BYTE*)&inet_address;

				m_nCameraID = ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iID;
				m_strPassword = ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_strPassword;
				m_ipCamera.SetAddress(pbIP[0],pbIP[1],pbIP[2],pbIP[3]);
				m_nPort = ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_port;
				m_nFrameRate = ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iFrameRate;
				m_nBitrate = ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iBitrate;
				m_nQuality = ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iQualityRange;
				m_nHeight =	ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iHeight;
				m_nWidth = ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iWidth;
			}
		}
		else
		{
			CSite* ss = getStoredSite(site);
			if(ss)
			{
				DWORD inet_address = inet_addr((LPCTSTR)ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_ip);
				BYTE* pbIP = (BYTE*)&inet_address;

				m_nCameraID = ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iID;
				m_strPassword = ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_strPassword;
				m_ipCamera.SetAddress(pbIP[0],pbIP[1],pbIP[2],pbIP[3]);
				m_nPort = ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_port;
				m_nFrameRate = ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iFrameRate;
				m_nBitrate = ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iBitrate;
				m_nQuality = ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iQualityRange;
				m_nHeight =	ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iHeight;
				m_nWidth = ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iWidth;
			}
		}
		UpdateData(FALSE);
		
		SetCurState(TRUE, TRUE);
	}
	else
	{
		UpdateData(TRUE);

		m_strCamera = camera;

		m_nBitrate = 500;
		m_nCameraID = 0;
		m_nPort = 777;
		m_nFrameRate = 15;
		m_nHeight = 240;
		m_nQuality = 0;
		m_nWidth = 320;
		m_ipCamera.SetAddress(127,0,0,1);
		m_strPassword = "none";
		UpdateData(FALSE);

		SetCurState(FALSE);
	}
}

void CRMSEditCameraDlg::OnAddcamera() 
{
	UpdateData(TRUE);

	in_addr inet_address;
	BYTE* pbIP = (BYTE*)&inet_address.S_un.S_addr;
	m_ipCamera.GetAddress(pbIP[0],pbIP[1],pbIP[2],pbIP[3]);
	if(m_bTypeCamera)
	{
		CSite* ls = getLiveSite(m_strSite);
		if(ls)
		{
			if (m_strPassword=="") m_strPassword="none";
			strcpy(ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_ip, inet_ntoa(inet_address));
			strcpy(ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_strPassword, m_strPassword);
			ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iID = m_nCameraID;
			ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_port = m_nPort;
			ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iFrameRate = m_nFrameRate;
			ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iBitrate = m_nBitrate;
			ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iQualityRange = m_nQuality;
			ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iHeight = m_nHeight;
			ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iWidth = m_nWidth;
#ifndef VC10
			CChannelInfo* ci = getInfoLive(getActiveChannel());
			if(ci) ci->m_bAdd = true;
#endif
		}
	}
	else
	{
		CSite* ss = getStoredSite(m_strSite);
		if(ss)
		{
			strcpy(ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_ip, inet_ntoa(inet_address));
			strcpy(ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_strPassword, m_strPassword);
			ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iID = m_nCameraID;
			ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_port = m_nPort;
			ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iFrameRate = m_nFrameRate;
			ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iBitrate = m_nBitrate;
			ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iQualityRange = m_nQuality;
			ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iHeight = m_nHeight;
			ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iWidth = m_nWidth;
#ifndef VC10
			CChannelInfo* si = getInfoStored(getActiveChannel());
			if(si) si->m_bAdd = true;
#endif
		}
	}

	if(m_cmbxSite.FindStringExact(-1,m_strSite) == CB_ERR)
	{
		m_cmbxSite.AddString(m_strSite);
	}
	if(m_cmbxBuilding.FindStringExact(-1,m_strBuilding) == CB_ERR)
	{
		m_cmbxBuilding.AddString(m_strBuilding);
	}
	if(m_cmbxRoom.FindStringExact(-1,m_strRoom) == CB_ERR)
	{
		m_cmbxRoom.AddString(m_strRoom);
	}
	if(m_cmbxCamera.FindStringExact(-1,m_strCamera) == CB_ERR)
	{
		m_cmbxCamera.AddString(m_strCamera);
	}

	m_bEnableSubmit = TRUE;
	SetCurState(TRUE, TRUE);
}

void CRMSEditCameraDlg::OnClear() 
{
	UpdateData(TRUE);
	m_nBitrate = 500;
	m_nCameraID = 0;
	m_nPort = 777;
	m_nFrameRate = 15;
	m_nHeight = 240;
	m_nQuality = 0;
	m_nWidth = 320;
	m_ipCamera.SetAddress(127,0,0,1);
	m_strPassword = "none";
	UpdateData(FALSE);
}


void CRMSEditCameraDlg::OnSelendokType() 
{
	m_cmbxSite.ResetContent();
	m_cmbxBuilding.ResetContent();
	m_cmbxRoom.ResetContent();
	m_cmbxCamera.ResetContent();

	m_cmbxBuilding.EnableWindow(FALSE);
	m_cmbxRoom.EnableWindow(FALSE);
	m_cmbxCamera.EnableWindow(FALSE);

	SetCurState(FALSE);

	map<CString,CSite>::const_iterator s;
	m_cmbxSite.AddString("none");

	if(m_cmbxType.GetCurSel() == m_cmbxType.FindString(-1,"Live"))
	{
		m_bTypeCamera = TRUE;
		map<CString, CSite>* ls = getSitesLive();
		if(ls)
		{
			for(s = ls->begin(); s != ls->end(); ++s)
			{
				m_cmbxSite.AddString((LPCTSTR)s->first);
			}
		}
	}
	else
	{
		m_bTypeCamera = FALSE;
		map<CString, CSite>* ss = getSitesStored();
		if(ss)
		{
			for(s = ss->begin(); s != ss->end(); ++s)
			{
				m_cmbxSite.AddString((LPCTSTR)s->first);
			}
		}
	}
	m_cmbxSite.SetCurSel(m_cmbxSite.FindStringExact(-1, "none"));
}

void CRMSEditCameraDlg::EnableSubmit() 
{
	if(m_bEdit)
	{
		m_bEnableSubmit = TRUE;
		SetCurState(TRUE, TRUE);
	}
}

void CRMSEditCameraDlg::OnDelete() 
{
	int nCurrentSite = m_cmbxSite.GetCurSel();
	CString site;
	m_cmbxSite.GetLBText(nCurrentSite, site);

	int nCurrentBuilding = m_cmbxBuilding.GetCurSel();
	CString building;
	m_cmbxBuilding.GetLBText(nCurrentBuilding, building);

	int nCurrentRoom = m_cmbxRoom.GetCurSel();
	CString room;
	m_cmbxRoom.GetLBText(nCurrentRoom, room);
	
	int nCurrentCamera = m_cmbxCamera.GetCurSel();
	CString camera;
	m_cmbxCamera.GetLBText(nCurrentCamera, camera);

	if(m_bTypeCamera)
	{
		CSite* lsite = getLiveSite(m_strSite);
		if(lsite)
		{
			lsite->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras.erase(camera);

			if(lsite->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras.empty())
			{
				m_cmbxRoom.DeleteString(m_cmbxRoom.FindString(-1,m_strRoom));
				m_cmbxRoom.SetCurSel(m_cmbxRoom.FindString(-1,"none"));

				lsite->m_Buildings[m_strBuilding].m_Rooms.erase(m_strRoom);
				if(lsite->m_Buildings[m_strBuilding].m_Rooms.empty())
				{
					m_cmbxBuilding.DeleteString(m_cmbxBuilding.FindString(-1,m_strBuilding));
					m_cmbxBuilding.SetCurSel(m_cmbxBuilding.FindString(-1,"none"));

					lsite->m_Buildings.erase(m_strBuilding);
					if(lsite->m_Buildings.empty())
					{
						m_cmbxSite.DeleteString(m_cmbxSite.FindString(-1,m_strSite));
						m_cmbxSite.SetCurSel(m_cmbxSite.FindString(-1,"none"));

						map<CString, CSite>* ls = getSitesLive();
						if(ls) ls->erase(m_strSite);
					}
				}
			}
		}

#ifndef VC10
		for(int i = 0; i<4; i++)
		{
			CChannelInfo* ci = getInfoLive(i);
			if( ci && (ci->m_site == m_strSite) &&
					  (ci->m_building == m_strBuilding) &&
					  (ci->m_room == m_strRoom) &&
					  (ci->m_camera == m_strCamera))
			{
				ci->m_bDelete = true;
			}
		}
#endif
	}
#ifndef VC10
	else
	{
		CSite* ssite = getStoredSite(m_strSite);
		if(ssite)
		{
			ssite->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras.erase(camera);

			if(ssite->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras.empty())
			{
				m_cmbxRoom.DeleteString(m_cmbxRoom.FindString(-1,m_strRoom));
				m_cmbxRoom.SetCurSel(m_cmbxRoom.FindString(-1,"none"));

				ssite->m_Buildings[m_strBuilding].m_Rooms.erase(m_strRoom);
				if(ssite->m_Buildings[m_strBuilding].m_Rooms.empty())
				{
					m_cmbxBuilding.DeleteString(m_cmbxBuilding.FindString(-1,m_strBuilding));
					m_cmbxBuilding.SetCurSel(m_cmbxBuilding.FindString(-1,"none"));

					ssite->m_Buildings.erase(m_strBuilding);
					if(ssite->m_Buildings.empty())
					{
						m_cmbxSite.DeleteString(m_cmbxSite.FindString(-1,m_strSite));
						m_cmbxSite.SetCurSel(m_cmbxSite.FindString(-1,"none"));

						map<CString, CSite>* ss = getSitesStored();
						if(ss) ss->erase(m_strSite);
					}
				}
			}
		}
		
		for(int i = 0; i<4; i++)
		{
			CChannelInfo* si = getInfoStored(i);
			if(si)
			{
				if(	(si->m_site == m_strSite) &&
					(si->m_building == m_strBuilding) &&
					(si->m_room == m_strRoom) &&
					(si->m_camera == m_strCamera))
				{
					si->m_bDelete = true;
				}
			}
		}
	}

#endif
	m_cmbxCamera.DeleteString(m_cmbxCamera.FindString(-1,camera));
	m_cmbxCamera.SetCurSel(m_cmbxCamera.FindString(-1,"none"));
	m_bEnableSubmit = TRUE;
	SetCurState(FALSE, FALSE);
}

void CRMSEditCameraDlg::OnSubmit() 
{
	FILE* f = getLiveCamFile("w+");

	map<CString,CSite>::const_iterator s;
	map<CString,CBuilding>::const_iterator b;
	map<CString,CRoom>::const_iterator r;
	map<CString,CInfo>::const_iterator c;

	if(f)
	{
		map<CString, CSite>* ls = getSitesLive();
		if(ls)
		{
			for(s = ls->begin(); s != ls->end(); s++)
			{
				for(b = (s->second).m_Buildings.begin(); b != (s->second).m_Buildings.end(); b++)
				{
					for(r = (b->second).m_Rooms.begin(); r != (b->second).m_Rooms.end(); r++)
					{
						for(c = (r->second).m_Cameras.begin(); c != (r->second).m_Cameras.end(); c++)
						{
							fprintf(f,"%s %s %s %s %i %s %s %i %i %i %i %i %i\n",
									(LPCTSTR)s->first, (LPCTSTR)b->first, (LPCTSTR)r->first, (LPCTSTR)c->first,
									(int)(c->second).m_iID, (LPCTSTR)(c->second).m_strPassword,
									(LPCTSTR)(c->second).m_ip, (int)(c->second).m_port, (int)(c->second).m_iFrameRate,
									(int)(c->second).m_iQualityRange, (int)(c->second).m_iBitrate,
									(int)(c->second).m_iWidth, (int)(c->second).m_iHeight);
						}
					}
				}
			}
		}
		fclose(f);
	}

#ifndef _NET_VC_
	f = getStoredCamFile("w+");

	if(f)
	{
		map<CString, CSite>* ss = getSitesStored();
		if(ss)
		{
			for(s = ss->begin(); s != ss->end(); s++)
			{
				for(b = (s->second).m_Buildings.begin(); b != (s->second).m_Buildings.end(); b++)
				{
					for(r = (b->second).m_Rooms.begin(); r != (b->second).m_Rooms.end(); r++)
					{
						for(c = (r->second).m_Cameras.begin(); c != (r->second).m_Cameras.end(); c++)
						{
							fprintf(f,"%s %s %s %s %i %s %s %i %i %i %i %i %i\n",
									(LPCTSTR)s->first, (LPCTSTR)b->first, (LPCTSTR)r->first, (LPCTSTR)c->first,
									(int)(c->second).m_iID, (LPCTSTR)(c->second).m_strPassword,
									(LPCTSTR)(c->second).m_ip, (int)(c->second).m_port, (int)(c->second).m_iFrameRate,
									(int)(c->second).m_iQualityRange, (int)(c->second).m_iBitrate,
									(int)(c->second).m_iWidth, (int)(c->second).m_iHeight);
						}
					}
				}
			}
		}
		fclose(f);
	}
#endif //ndef _NET_VC_

#ifndef VC10
	for(int i = 0; i<4; i++)
	{
		CChannelInfo* ci = getInfoLive(i);
		if(ci)
		{
			if(ci->m_bDelete)
			{
				ci->m_site = "none";
				ci->m_building = "none";
				ci->m_room = "none";
				ci->m_camera = "none";
				ci->m_bActive = false;

				sessionDisconnect(i);
			}

			if(ci->m_bChange)
			{
				CString temp;
				temp = ci->m_camera;
				ci->m_camera = "none";
				ci->m_bActive = false;

				sessionDisconnect(i);
				
				ci->m_camera = temp;
				ci->m_bActive = true;
				sessionConnect(i);
			}
		}
	}
	CLivePage* lp = (CLivePage*)getLivePage();
	if(lp) lp->SetActiveChannel(getActiveChannel());
	for(i = 0; i<4; i++)
	{
		CChannelInfo* si = getInfoStored(i);
		if(si)
		{
			if(si->m_bDelete)
			{
				si->m_site = "none";
				si->m_building = "none";
				si->m_room = "none";
				si->m_camera = "none";
				si->m_bActive = false;

				sessionDisconnect(i+4);
			}

			if(si->m_bChange)
			{
				CString temp;
				temp = si->m_camera;
				si->m_camera = "none";
				si->m_bActive = false;

				sessionDisconnect(i+4);
				CStoredPage* sp = (CStoredPage*)getStoredPage();
				if(sp) sp->m_player[i].StopPlayer();

				si->m_camera = temp;
				si->m_bActive = true;
				CChannelInfo* pinf = si;
				sessionConnect(i+4);
				CSite* ss = getStoredSite(pinf->m_site);
				if(ss)
				{
					sp = (CStoredPage*)getStoredPage();
					if(sp) sp->m_player[i].StartPlayer(ss->m_Buildings[pinf->m_building].m_Rooms[pinf->m_room].m_Cameras[pinf->m_camera].m_iWidth,
					ss->m_Buildings[pinf->m_building].m_Rooms[pinf->m_room].m_Cameras[pinf->m_camera].m_iHeight);
				}
			}
		}
	}

	CStoredPage* sp = (CStoredPage*)getStoredPage();
	if(sp) sp->SetActiveChannel(getActiveChannel());
	
	for(i = 0; i<4; i++)
	{
		CChannelInfo* ci = getInfoLive(i);
		if(ci)
		{
			ci->m_bAdd = false;
			ci->m_bChange = false;
			ci->m_bDelete = false;
		}

		CChannelInfo* si = getInfoStored(i);
		if(si)
		{
			si->m_bAdd = false;
			si->m_bChange = false;
			si->m_bDelete = false;
		}
	}
#endif

	EndDialog(IDOK);
}

void CRMSEditCameraDlg::OnFieldchangedCameraip(NMHDR* pNMHDR, LRESULT* pResult) 
{
	if(m_bEdit)
	{
		m_bEnableSubmit = TRUE;
		SetCurState(TRUE, TRUE);
		UpdateMap();
	}
		
	*pResult = 0;
}

void CRMSEditCameraDlg::OnCancel() 
{
	char site[128];
	char building[128];
	char room[128];
	char camera[128];
	char password[128];
	int ID = 0;
	char ip[128];
	int port = 0;
	int FrameRate = 0;
	int QualityRange = 0;
	int Bitrate = 0;
	int Width = 0;
	int Height = 0;

	map<CString,CSite>::const_iterator s;
	map<CString,CBuilding>::const_iterator b;
	map<CString,CRoom>::const_iterator r;

	map<CString, CSite>* ls = getSitesLive();
	if(ls)
	{
		for(s = ls->begin(); s != ls->end(); s++)
		{
			for(b = (s->second).m_Buildings.begin(); b != (s->second).m_Buildings.end(); b++)
			{
				for(r = (b->second).m_Rooms.begin(); r != (b->second).m_Rooms.end(); r++)
				{
					((CRoom)(r->second)).m_Cameras.clear();
				}
				((CBuilding)(b->second)).m_Rooms.clear();
			}
			((CSite)(s->second)).m_Buildings.clear();
		}
		ls->clear();
	}

	FILE* file_in = getLiveCamFile("r");

	if(file_in)
	{
		while(!feof(file_in))
		{
			strcpy(site,"none");strcpy(building,"none");strcpy(room,"none");
			strcpy(camera,"none");strcpy(password,"none");
			fscanf(file_in,"%s %s %s %s %i %s %s %i %i %i %i %i %i",site,building,room,camera,&ID,password,ip,&port,&FrameRate,&QualityRange,&Bitrate,&Width,&Height);
			if (strcmp(site,"none")==0) continue;
			CSite* ls = getLiveSite(site);
			if(ls)
			{
				ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iID = ID;
				strcpy(ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_strPassword, password);
				strcpy(ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_strPassword, password);
				strcpy(ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_ip, ip);
				ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_port = port;
				ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iFrameRate = FrameRate;
				ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iQualityRange = QualityRange;
				ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iBitrate = Bitrate;
				ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iHeight = Height;
				ls->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iWidth = Width;
			}
		}
		fclose(file_in);
	}

#ifndef _NET_VC_
	map<CString, CSite>* ss = getSitesStored();
	if(ss)
	{
		for(s = ss->begin(); s != ss->end(); s++)
		{
			for(b = (s->second).m_Buildings.begin(); b != (s->second).m_Buildings.end(); b++)
			{
				for(r = (b->second).m_Rooms.begin(); r != (b->second).m_Rooms.end(); r++)
				{
					((CRoom)(r->second)).m_Cameras.clear();
				}
				((CBuilding)(b->second)).m_Rooms.clear();
			}
			((CSite)(s->second)).m_Buildings.clear();
		}
		ss->clear();
	}
	
	file_in = getStoredCamFile("r");

	if(file_in)
	{
		while(!feof(file_in))
		{
			strcpy(site,"none");strcpy(building,"none");strcpy(room,"none");
			strcpy(camera,"none");strcpy(password,"none");
			fscanf(file_in,"%s %s %s %s %i %s %s %i %i %i %i %i %i",site,building,room,camera,&ID,password,ip,&port,&FrameRate,&QualityRange,&Bitrate,&Width,&Height);
			if (strcmp(site,"none")==0) continue;
			CSite* ss = getStoredSite(site);
			if(ss)
			{
				ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iID = ID;
				strcpy(ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_strPassword, password);
				strcpy(ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_ip, ip);
				ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_port = port;
				ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iFrameRate = FrameRate;
				ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iQualityRange = QualityRange;
				ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iBitrate = Bitrate;
				ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iHeight = Height;
				ss->m_Buildings[building].m_Rooms[room].m_Cameras[camera].m_iWidth = Width;
			}
		}

		fclose(file_in);
	}	
#endif //ndef _NET_VC_

#ifndef VC10
	for(int i = 0; i<4; i++)
	{
		CChannelInfo* ci = getInfoLive(i);
		if(ci)
		{
			ci->m_bAdd = false;
			ci->m_bChange = false;
			ci->m_bDelete = false;
		}

		CChannelInfo* si = getInfoStored(i);
		if(si)
		{
			si->m_bAdd = false;
			si->m_bChange = false;
			si->m_bDelete = false;
		}
	}
#endif
	
	CDialog::OnCancel();
}

void CRMSEditCameraDlg::UpdateMap()
{
	if(UpdateData(TRUE))
	{
		in_addr inet_address;
		BYTE* pbIP = (BYTE*)&inet_address.S_un.S_addr;
		m_ipCamera.GetAddress(pbIP[0],pbIP[1],pbIP[2],pbIP[3]);
		if(m_bTypeCamera)
		{
			CSite* ls = getLiveSite(m_strSite);
			if(ls)
			{
				if (m_strPassword=="") m_strPassword="none";
				strcpy(ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_ip, inet_ntoa(inet_address));
				ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iID =  m_nCameraID;
				strcpy(ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_strPassword, m_strPassword);
				ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_port = m_nPort;
				ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iFrameRate = m_nFrameRate;
				ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iBitrate = m_nBitrate;
				ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iQualityRange = m_nQuality;
				ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iHeight = m_nHeight;
				ls->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iWidth = m_nWidth;
			}

#ifndef VC10
			for(int i=0; i<4; i++)
			{
				CChannelInfo* ci = getInfoLive(i);
				if(ci)
				{
					if(ci->m_bActive)
					{
						if(	(ci->m_site == m_strSite) && 
							(ci->m_building == m_strBuilding) &&
							(ci->m_room == m_strRoom) &&
							(ci->m_camera == m_strCamera))
						{
							ci->m_bActive = false;
							sessionDisconnect(i);

							ci->m_camera = m_strCamera;
							ci->m_bActive = true;

							CChannelInfo* pinf = ci;
							CSite* ls = getLiveSite(pinf->m_site);
							if(ls) setCameraInfo(i, ls->m_Buildings[pinf->m_building].m_Rooms[pinf->m_room].m_Cameras[pinf->m_camera]);
							sessionConnect(i);
						}
					}
				}
			}
			for(i = 0; i<4; i++)
			{
				CChannelInfo* ci = getInfoLive(i);
				if(ci)
				{
					if(	(ci->m_site == m_strSite) &&
						(ci->m_building == m_strBuilding) &&
						(ci->m_room == m_strRoom) &&
						(ci->m_camera == m_strCamera))
					{
						ci->m_bChange = true;
					}
				}
			}
#endif
		}
#ifndef VC10
		else
		{
			CSite* ss = getStoredSite(m_strSite);
			if(ss)
			{
				strcpy(ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_ip, inet_ntoa(inet_address));
				ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iID = m_nCameraID;
				strcpy(ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_strPassword, m_strPassword);
				ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_port = m_nPort;
				ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iFrameRate = m_nFrameRate;
				ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iBitrate = m_nBitrate;
				ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iQualityRange = m_nQuality;
				ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iHeight = m_nHeight;
				ss->m_Buildings[m_strBuilding].m_Rooms[m_strRoom].m_Cameras[m_strCamera].m_iWidth = m_nWidth;
			}

			for(int i=0; i<4; i++)
			{
				CChannelInfo* si = getInfoStored(i);
				if(si)
				{
					if(si->m_bActive)
					{
						if(	(si->m_site == m_strSite) && 
							(si->m_building == m_strBuilding) &&
							(si->m_room == m_strRoom) &&
							(si->m_camera == m_strCamera))
						{
							si->m_bActive = false;
							sessionDisconnect(i+4);
							CStoredPage* sp = (CStoredPage*)getStoredPage();
							if(sp) sp->m_player[i].StopPlayer();

							si->m_camera = m_strCamera;
							si->m_bActive = true;

							CChannelInfo* pinf = si;
							CSite* ss = getStoredSite(pinf->m_site);
							if(ss) setCameraInfo(i+4, ss->m_Buildings[pinf->m_building].m_Rooms[pinf->m_room].m_Cameras[pinf->m_camera]);
							sessionConnect(i+4);
						}
					}
				}
			}
			for(i = 0; i<4; i++)
			{
				CChannelInfo* si = getInfoStored(i);
				if(si)
				{
					if(	(si->m_site == m_strSite) &&
						(si->m_building == m_strBuilding) &&
						(si->m_room == m_strRoom) &&
						(si->m_camera == m_strCamera))
					{
						si->m_bChange = true;
					}
				}
			}

		}
#endif
	}
}

void CRMSEditCameraDlg::OnKillfocus() 
{
	UpdateMap();
}
